package com.emep.changzhi.analyse.service.basics;

import com.emep.changzhi.analyse.entity.basics.SysFactorInfo;
import com.emep.changzhi.analyse.repository.basics.SysFactorInfoReposityory;
import com.emep.changzhi.analyse.utils.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author Wangcc
 * @version V1.0
 * @Description: 因子信息业务层
 * @date 2018/2/26 14:06
 */
@Service
public class SysFactorInfoService {
    
    
    @Resource
    private SysFactorInfoReposityory sysFactorInfoReposityory;

    private static final Logger logger = LoggerFactory.getLogger(SysFactorInfoService.class);


    /**
     *
     * 不含查询条件的分页获取数据
     *
     * @param pageNo
     * @param pageSize
     * @return
     * @throws Exception
     */
    public Page<SysFactorInfo> findAll(Integer pageNo, Integer pageSize){
        try {
            Pageable pageable = new PageRequest(pageNo-1, pageSize);
            return  sysFactorInfoReposityory.findAll(pageable);
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
            return null;
        }
    }

    /**
     *
     * 新增因子信息
     *
     * @param payload
     * @return
     * @throws Exception
     */
    public SysFactorInfo save(Map<String, Object> payload) {
        SysFactorInfo SysFactorInfo = null;
        try {
            SysFactorInfo SysFactorInfo1 = (SysFactorInfo) MapUtils.convertMap(SysFactorInfo.class,payload);
            SysFactorInfo = sysFactorInfoReposityory.save(SysFactorInfo1);
        }catch (Exception e){
            logger.error(e.getLocalizedMessage());
        }
        return SysFactorInfo;
    }


    /**
     *
     * 根据id查询因子信息
     *
     * @param id
     * @return
     */
    public SysFactorInfo findId(String id){
        SysFactorInfo sysFactorInfo = null;
        try {
            sysFactorInfo = sysFactorInfoReposityory.findSysFactorInfoById(id);
        }catch (Exception e){
            logger.error(e.getLocalizedMessage());
        }
        return sysFactorInfo;
    }

    /**
     *
     * 根据id 删除因子信息
     *
     * @param id
     */
    public Boolean delete(String id) {
        Boolean result = false;
        try {
            Integer result1 = sysFactorInfoReposityory.deleteById(id);
            if(result1 == 1){
                result = true;
            }
        }catch (Exception e){
            logger.error(e.getLocalizedMessage());
        }
        return result;
    }

    /**
     *查询所有因子信息
     *
     * @return List<SysFactorInfo>
     */
    public List<SysFactorInfo> findAll() {
        List<SysFactorInfo> SysFactorInfos = null;
        try {
            SysFactorInfos =  sysFactorInfoReposityory.findAll();
        }catch (Exception e){
            logger.error(e.getLocalizedMessage());
        }
        return SysFactorInfos;
    }

}
